<?php
/**
 * Created by Chongyi
 * Date: 2015/10/10 0010
 * Time: 下午 2:45
 */

// 验证码获取路由
Route::get('captcha/{conf?}', [
    'as' => 'captcha.generate',
    function ($config = 'default') {
        return Captcha::create($config);
    },
])->where('conf', '[-A-Za-z0-9]+');


Route::get('temp/image/{token}', [
    'as' => 'temp.image',
    function ($token) {
        try {
            $instance = \App\B2BProject\AttachmentTempFiles::getFileByToken($token);

            $file = new SplFileObject(base_path($instance->local_path), 'r');

            header('Content-Type: ' . $instance->mime_type);
            $fd = fopen('php://output', 'w');

            while (!$file->eof()) {
                fwrite($fd, $file->fread(1024));
            }
            fclose($fd);

        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return response('Not found', 404);
        }

    }
]);

Route::get('private/image/{id}', [
    'as'         => 'private.image',
    'middleware' => 'auth',
    function ($id) {
        try {
            $file = \App\B2BProject\AttachmentPrivateFiles::findOrFail($id);

            header('Content-Type: ' . $file->mime_type);

            $file->readToBuffer();

        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return response('Not found', 404);
        }
    }
]);

Route::post('temp/image', [
    'as' => 'temp.image.upload',
    function (\Illuminate\Http\Request $request) {
        if ($request->hasFile('temp')) {
            $file = $request->file('temp');

            $instance = \App\B2BProject\AttachmentTempFiles::setNewUploadFile($file);

            return response()->json(['data' => route('temp.image', [$instance->token])]);
        }

        return response('Not found', 404);
    }
]);

Route::controller('area/list', 'AreaGetterController',
    ['getProvince' => 'area.province', 'postCity' => 'area.city', 'postCounty' => 'area.county']);

Route::controller('goods/primary-category', 'GoodsCategoryGetterController',
    [
        'getIndex'         => 'goods.primary-category',
        'getFirstCategory' => 'goods.primary-category.first.category',
        'getSpecial' => 'goods.primary-category.special',
        'postCategory'     => 'goods.primary-category.category',

    ]);

Route::controller('goods/brand', 'GoodsBrandCategoryGetterController',
    [
        'getIndex'       => 'goods.brand',
        'getGrantBrands' => 'goods.grant.brands'
    ]);



